var design_cell_id = $.trim($("#design_cell_id").val());
var pnum=1;
var select_field = $.trim($("#select_fields").val());
var con = "";
var param = {};
var initData = function(num){
	var url = base+"/projectCenter/designcell/designCellTable";
    param["page_num"] = num;
    param["page_size"] = "10";
    param["project_id"] = project_id;
    param["design_cell_id"] = design_cell_id;
	$.ajax({
        url: url,
        data: param,
        success: function (data, textStatus, jqXHR) {
        	if(data != null){
                var pageCount = data.data_list.pager.pageCount;
                var dataObj ={
                    "list":data.data_list.list,
                    "countList":data.count_list,
                    "dataTool":{
                        "title":{
                        },
                        "isCount":true,
                        "editField":{},
                        "titleCheckbox":false
                    }
                };
                var title_list = data.title_list;
                var title = dataObj.dataTool.title;
                var edit = dataObj.dataTool.editField;
                $.each(title_list,function(index,value){
                    var field_name = title_list[index].field_name;
                    var field_comment = title_list[index].field_comment;
                    if (field_name!="id"&& field_name!="project_id") {
                        if (title_list[index].widget_type!="hidden"){
                            title[field_name] = field_comment;
                            var editField = {};
                            editField["field"] = field_name;
                            editField["type"] = "text";
                            edit[index]=editField;
                        }
                    }
                });
                edit = JSON.stringify(edit);
                head_data_list = title; //将表头作为全局表里保存以便编辑操作
                showPageTable('view', dataObj,num,pageCount,"initData");
        	}
        },
        error: function () {
        }
    });
	
}
var celledit = function (obj,key,value,that,type,val) {
    if (type=='text') {
        var t_name = $.trim($("#t_name").val());
        if(!$(that).is('.input')){
            $(that).addClass('input').html('<input type="text" id="'+key+'" value="'+ value +'" />').find('input').focus().blur(function(){
                if (value!=$("#"+key).val()) {
                    $.ajax({
                        url:base+"/projectCenter/commonupdate/updateOneField/",
                        data:{"record_id":obj.id,"t_name":t_name,"field_name":key,"value":$("#"+key).val()},
                        success:function(data,status){
                            initData(pnum);
                        }
                    });
                }
                $(that).removeClass('input').html($("#"+key).val());
            });
        }
    } else if (type=='select') {
        var t_name = $.trim($("#t_name").val());
        var str = val.split("-");
        var soption = "";
        for (var i=0;i<str.length;i++) {
            if (""!=str[i]) {
                if (value==str[i].split("_")[1]) {
                    soption+="<option value='"+str[i].split("_")[0]+"' selected>"+str[i].split("_")[1]+"</option>";
                } else {
                    soption+="<option value='"+str[i].split("_")[0]+"'>"+str[i].split("_")[1]+"</option>";
                }
            }
        }
        if(!$(that).is('.select')){
            var html = "<select id='"+key+"' name='"+key+"' style='display:block;'>"+soption+"</select>";
            $(that).addClass('select').html(html).find('select').focus().blur(function(){
                if (value!=$("#"+key).val()) {
                    $.ajax({
                        url:base+"/projectCenter/commonupdate/updateOneField/",
                        data:{"record_id":obj.id,"t_name":t_name,"field_name":key,"value":$("#"+key).val()},
                        success:function(data,status){
                            initData(pnum);
                        }
                    });
                }
                $(that).removeClass('select').html($("#"+key).val());
            });
        }
    }

}
var showButton = function(){
    var button_html = ' <div class="btn-team">'+
        ' <button class="btn btn-default" id="caculate" onclick="caculate()">一键计算</button>';
    button_html += ' <button class="btn btn-default" id="download" onclick="downloadExcel()">下载</button>';
    var field_comment = select_field.split(",");
    var select_html = '<div class="layui-input-inline"><div class="subway-select"> ';
    for (var i=0;i<field_comment.length;i++) {
        var field = field_comment[i].split("-");
        if (""!=field[0]) {
            select_html +='<select id="'+field[0]+'"><option value="">请选择'+field[1]+'</option></select>';
        }
    }
    select_html += '</div></div>';
    $(".btn-team").remove();
    $("#div_content").prepend(button_html+select_html);
}

//初始化设备类型
var _initSelect = function(field_name,field_cname){
    $.post(base+"/projectCenter/designcell/initSelect/"+$("#t_name").val()+"/"+field_name+"/"+project_id,{},
        function(data,status){
        debugger
            if(data != null && data.length>0){
                debugger
                var optionstring = "";
                $.each(data, function(i,item){
                    optionstring += '<option value="'+item[field_name]+'">'+item[field_name]+'</option>';
                });
                $("#"+field_name).html('<option value="">请选择'+field_cname+'</option>' + optionstring);
            }else{
                $("#"+field_name).html('<option value="">请选择'+field_cname+'</option>');
            }
        });
}

var select_change = function(event){
    var map = event.data;
    var val = $("#"+map["field_name"]).children('option:selected').val();
    var text = $("#"+map["field_name"]).children('option:selected').text();
    param[map["field_name"]] = val;
    initData(1);
}

var downloadExcel = function(){
    var url = base+"/projectCenter/designcell/exportJsData/"+JSON.stringify(param);
    window.open(url,"_self");
    return false;
}

var caculate = function(){
	//debugger
    layer.confirm('确认开始计算?',{btn : [ '确定', '取消' ]},function(index){
        layer.close(index);
        $.ajax({
            url:base+"/projectCenter/designcell/caculate/"+design_cell_id+"/"+project_id,
            data:{},
            success:function(data,status){
                if(data!=""&&data!=null){
                    if(data.statusCode == 0){
                        layer.msg('计算成功', {icon: 1,time:2000});
                    }else{
                        layer.msg(data.message, {icon: 2,time:2000});
                    }
                }else{
                    layer.msg('计算失败!', {icon: 2,time:2000});
                }
                initData(1);
            }
        });
    });
}

$(document).ready(function(){
	showButton();
	initData(1);
    var field_comment = select_field.split(",");
    for (var i=0;i<field_comment.length;i++) {
        var field = field_comment[i].split("-");
        if (""!=field[0]) {
            _initSelect(field[0],field[1]);
            var p = {"field_name":field[0]};
            $("#"+field[0]).change(p,select_change);
        }
    }
});
